<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"></meta>
<title>留言板</title>

</head>
<body>
	<h3>留言列表</h3>
	<form method="post" action="search.php">
		搜索: <input name="search" type="text" value="<?=$_POST['search']?>" />
		<input type="submit" name="submit" value="模糊搜索" /> <br /> <br />
	</form>
	<input type="button" onclick="window.location.href='admin.php'"
		value="留言管理"></input>
	<br />
	<br />
<?php
// 引用相关文件
require ("./core/conn.php");
require ("./core/config.php");
// 确定当前页数 $p 参数
$p = $_GET['p'] ? $_GET['p'] : 1;
// 数据指针
$offset = ($p - 1) * $pagesize;
$searchtext = $_POST['search'];
$query_sql = "SELECT * FROM msg WHERE `nickname` LIKE '%$searchtext%' OR `email` LIKE '%$searchtext%' OR `content` LIKE '%$searchtext%' ORDER BY id DESC LIMIT  $offset , $pagesize";
$result = mysqli_query($conn, $query_sql);
// 如果出现错误并退出
if (! $result)
    exit('查询数据错误：' . mysqli_error());

// 循环输出
while (($gb_array = mysqli_fetch_array($result)) != false) {
    $content = nl2br($gb_array['content']);
    echo $gb_array['nickname'], '&nbsp;';
    echo '发表于：' . date("Y-m-d H:i:s", $gb_array['createtime']) . '<br />';
    echo '内容：', nl2br($gb_array['content']), '<br /><br />';
    
    if (! empty($gb_array['replytime'])) {
        echo '----------------------------<br />';
        echo '最后修改于：', date("Y-m-d H:i:s", $gb_array['replytime']), '<br />';
        echo nl2br($gb_array['reply']), '<br /><br />';
    }
    ?>
	<form id="form1" name="form1" method="post" action="editmsg.php">
		<input name="id" type="hidden" value="<?=$gb_array['id']?>" /> <input
			type="submit" name="submit" value="修改留言" /> <a
			href="editmsg.php?action=delete&id=<?=$gb_array['id']?>"
			onClick="return confirm('确定删除?');">删除留言</a>
	</form>
    <?php
    
    echo '<hr />';
}

// 计算留言页数
$count_result = mysqli_query($conn, "SELECT count(*) FROM msg WHERE `nickname` LIKE '%$searchtext%' OR `email` LIKE '%$searchtext%' OR `content` LIKE '%$searchtext%'");
$count_array = mysqli_fetch_array($count_result);
$pagenum = ceil($count_array['count(*)'] / $pagesize);
echo '共 ', $count_array['count(*)'], ' 条留言';
if ($pagenum > 1) {
    for ($i = 1; $i <= $pagenum; $i ++) {
        if ($i == $p) {
            echo '&nbsp;[', $i, ']';
        } else {
            echo '&nbsp;<a href="search.php?action=search&text=' . $_GET['search'] . '&p=', $i, '">' . $i . '</a>';
        }
    }
}
?>


	<br />
	<br />
	<input type="button" onclick="window.location.href='add.php'"
		value="点击留言"></input>

</body>
</html>